/*
 * Created on Wed Oct 11 2017
 * Copyright (c) 2017 jianglinjie
 */
'use strict';
import CaptchaService from '../captcha';
import * as TestConst from './TestConst';
import * as CaptchaBusiness from '../../constants/CaptchaBusiness';
import AuthService from '../auth';
import { md5 } from '../../utils/StringUtils';
import { UserInfo } from '../../serviceTypes';

test('Account_Login', async () => {
  await AuthService.loginWithAccount(
      TestConst.USER_NAME, 
      TestConst.USER_NAME_PWD);
  let token = await AuthService.getToken();
  expect(token).toBeTruthy();

  let curUser: UserInfo = await AuthService.getCurrentUserInfo();
  expect(curUser.uid).toBeTruthy();
  expect(curUser.userName).toMatch(TestConst.USER_NAME);
  expect(curUser.encryptPwd).toMatch(md5(TestConst.USER_NAME_PWD));
});

test('Phone_Login', async () => {
  let captchaResult = await CaptchaService.request({
    phone: TestConst.PHONE,
    business: CaptchaBusiness.LOGIN
  });

  await AuthService.loginWithPhoneCaptcha(
    TestConst.PHONE, 
    captchaResult.captcha);

  let token = await AuthService.getToken();
  expect(token).toBeTruthy();

  let curUser: UserInfo = await AuthService.getCurrentUserInfo();
  expect(curUser.uid).toBeTruthy();
  expect(curUser.phone).toMatch(TestConst.PHONE);
  expect(curUser.encryptPwd).toMatch(md5(TestConst.PHONE_PWD));
});

test('Auto_Login', async () => {
  await AuthService.loginWithAccount(
    TestConst.USER_NAME, 
    TestConst.USER_NAME_PWD);
  await AuthService.autoLogin();

  let token = await AuthService.getToken();
  expect(token).toBeTruthy();

  let curUser: UserInfo = await AuthService.getCurrentUserInfo();
  expect(curUser.uid).toBeTruthy();
  expect(curUser.userName).toMatch(TestConst.USER_NAME);
  expect(curUser.encryptPwd).toMatch(md5(TestConst.USER_NAME_PWD));
});

test('Logout', async () => {
  await AuthService.loginWithAccount(
    TestConst.USER_NAME, 
    TestConst.USER_NAME_PWD);
  
  let token1 = await AuthService.getToken();
  expect(token1).toBeTruthy();

  await AuthService.logout();
  let token2 = await AuthService.getToken();
  expect(token2).toBeFalsy();
});